package de.hwrBerlin.dataMining.view.widgets.combo;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

public class ExtendedComboBox {

	private Combo _combo;
	private List<Object> _items;
	private ComboShower _shower;
	private boolean _nullSelection;

	public ExtendedComboBox(Composite pParent, int pStyle, boolean pNullSelection, ComboShower pComboShower) {
		_nullSelection = pNullSelection;
		_combo = new Combo(pParent, pStyle);
		_items = new ArrayList<Object>();
		
		_shower = pComboShower;
	}

	public ExtendedComboBox(Composite pParent, int pStyle, boolean pNullSelection) {
		_nullSelection = pNullSelection;
		_combo = new Combo(pParent, pStyle);
		_items = new ArrayList<Object>();

		_shower = new ComboShower() {

			@Override
			public String getStringFromObject(Object pObject) {
				return pObject.toString();
			}
		};
	}

	public void setInput(Object[] pItems) {
		_items = new ArrayList<Object>();
		for (Object object : pItems) {
			_items.add(object);
		}
		_combo.removeAll();
		sortList();
		for (Object item : _items) {
			String itemString = _shower.getStringFromObject(item);
			_combo.add(itemString);
		}
		if (_nullSelection) {
			_combo.add("[Keine Auswahl]");
		}
		_combo.redraw();
	}

	public void remove(Object pObject) {
		if (pObject != null && _items.contains(pObject)) {
			int removeIndex = _items.indexOf(pObject);
			_items.remove(pObject);
			_combo.remove(removeIndex);
		}
	}

	public void add(Object pObject) {
		_items.add(pObject);
		sortList();
		_combo.add(_shower.getStringFromObject(pObject));
	}

	public Object getSelection() {
		int selectionIndex = _combo.getSelectionIndex();
		if (selectionIndex == -1) {
			return null;
		}
		if (selectionIndex >= _items.size()) {
			return null;
		}
		return _items.get(selectionIndex);
	}

	public void setSetlection(Object pObject) {
		int selectionIndex = _items.indexOf(pObject);
		_combo.select(selectionIndex);
	}

	public void addSelectionListener(SelectionListener pSelectionListener) {
		_combo.addSelectionListener(pSelectionListener);
	}

	public void removeSelectionListener(SelectionListener pSelectionListener) {
		_combo.removeSelectionListener(pSelectionListener);
	}

	public void redraw() {
		setInput(_items.toArray(new Object[0]));
	}

	private void sortList() {
		List<Object> sortedList = new ArrayList<Object>();
		while (_items.size() != 0) {
			Object smallestItem = null;
			for (Object item : _items) {
				if (smallestItem == null) {
					smallestItem = item;
				} else {
					if (_shower.getStringFromObject(smallestItem).compareTo(_shower.getStringFromObject(item)) > 0) {
						smallestItem = item;
					}
				}
			}
			_items.remove(smallestItem);
			sortedList.add(smallestItem);
		}
		_items = sortedList;
	}

	public void setLayoutData(Object pLayoutData) {
		_combo.setLayoutData(pLayoutData);
	}

	public Combo getCombo() {
		return _combo;
	}
}
